import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../controllers/example_list_controller.dart';
import '../../../../core/controllers/base_controller.dart';

class SimpleListPage extends StatelessWidget {
  const SimpleListPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final controller = Get.put(ExampleListController());
    
    return Scaffold(
      appBar: AppBar(
        title: const Text('简单列表'),
      ),
      body: GetBuilder<ExampleListController>(
        id: 'simple_list',
        builder: (controller) {
          print('Building UI with state: ${controller.viewState}');
          print('Items count: ${controller.items.length}');
          
          // 直接根据状态显示不同的UI
          switch (controller.viewState) {
          case ViewState.loading:
            return const Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  CircularProgressIndicator(),
                  SizedBox(height: 16),
                  Text('正在加载...'),
                ],
              ),
            );
          case ViewState.error:
            return Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  const Icon(Icons.error, size: 64, color: Colors.red),
                  const SizedBox(height: 16),
                  Text('加载失败: ${controller.errorMessage}'),
                  const SizedBox(height: 16),
                  ElevatedButton(
                    onPressed: () => controller.loadData(),
                    child: const Text('重试'),
                  ),
                ],
              ),
            );
          case ViewState.empty:
            return const Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Icon(Icons.inbox, size: 64, color: Colors.grey),
                  SizedBox(height: 16),
                  Text('暂无数据'),
                ],
              ),
            );
          case ViewState.success:
            return ListView.builder(
              itemCount: controller.items.length,
              itemBuilder: (context, index) {
                final item = controller.items[index];
                return Card(
                  margin: EdgeInsets.symmetric(horizontal: 16.w, vertical: 8.h),
                  child: ListTile(
                    title: Text(item.title),
                    subtitle: Text(item.description),
                    trailing: Text(item.id.toString()),
                  ),
                );
              },
            );
          default:
            return const Center(
              child: Text('初始状态'),
            );
        }
        },
      ),
    );
  }
}